package gf.client.controller;

import gf.client.utils.HttpUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;

/**
 * 文件数据
 *
 * @author Gaofeng
 * @since 2021/10/17 12:45
 **/

public class DataController {
    public static JSONObject getData(String uuid) throws Exception {
        HttpURLConnection connection = HttpUtils.creatUtil("http://127.0.0.1:8080/file/fileinfo?uuid=" + uuid, "GET");

        /*
         * 返回输入流
         */
        InputStream is = connection.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) != -1) {
            byteArrayOutputStream.write(buffer, 0, len);
        }
        String jsonString = byteArrayOutputStream.toString();
        byteArrayOutputStream.close();
        is.close();

        /*
         * 转换成json数据
         */
        return JSON.parseObject(jsonString);
    }
}
